/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

<template>
  <div class='entrance'>
    <!-- 顶部按钮区域 -->
    <div class='top-buttons' :style='{
      paddingTop: `${topMargin + 8}px`
    }'>
      <img src='../assets/home_pause.png' alt='' class='top-image' :style='{
        paddingLeft: `${margins.leftTopMargin + 16}px`
      }'>
      <div class='right-buttons-group' :style='{
        paddingRight: `${margins.rightTopMargin + 16}px`,
      }'>
        <img src='../assets/mute.png' alt='' class='top-image'>
        <img src='../assets/rotate.png' alt='' class='top-image'>
        <img src='../assets/setting.png' alt='' class='top-image'>
      </div>
    </div>

    <!-- 中间内容区域 -->
    <div class='middle-content'>
      <img src='../assets/home_icon.png' alt='' class='main_image'>
      <div class='button-container'>
        <img src='../assets/arrowhead.png' alt='' class='arrowhead'>
        <router-link to='/game' style='height:20px'>
          <img src='../assets/start.png' alt='' class='options'>
        </router-link>
      </div>
      <div class='button-container'>
        <img src='../assets/arrowhead.png' alt='' class='arrowhead' style='visibility:hidden'>
        <router-link to='/game' style='height:20px'>
          <img src='../assets/login.png' alt='' class='options'>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script lang='ts'>
import Vue from 'vue';
import { AreaUtils } from '../utils/AreaUtils';
import { AvoidAreaType } from '../common/windowCommon';

export default Vue.extend({
  name: 'HomeView',
  data() {
    return {
      margins: {
        leftTopMargin: 0,
        rightTopMargin: 0,
        leftBottomMargin: 0,
        rightBottomMargin: 0,
        leftCenterMargin: 0,
        rightCenterMargin: 0
      },
      topMargin: AreaUtils.getInstance().getTopMargin()
    };
  },
  methods: {
    reCulcAvoidArea() {
      AreaUtils.getAvoidAreaLocation().then((res) => {
        AreaUtils.getInstance().setAvoidValue(res.areaLocation, res.marginValue);
        this.margins = AreaUtils.getAllMargin();
      });
    },
    handleStatusBarChange(event: any) {
      const data = event.detail;
      const areaType = data.type;
      if (areaType === AvoidAreaType.TYPE_SYSTEM) {
        this.topMargin = AreaUtils.px2vp(data.area.topRect.height);
        AreaUtils.getInstance().setTopMargin(this.topMargin);
      }
      if (areaType === AvoidAreaType.TYPE_CUTOUT) {
        this.reCulcAvoidArea();
      }
      if (areaType === AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
        let bottomMargin = AreaUtils.px2vp(data.area.bottomRect.height);
        AreaUtils.getInstance().setBottomMargin(bottomMargin);
      }
    }
  },
  mounted() {
    this.reCulcAvoidArea();
    window.addEventListener('resize', this.reCulcAvoidArea);
    window.addEventListener('statusBarChange', this.handleStatusBarChange);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.reCulcAvoidArea);
    window.removeEventListener('statusBarChange', this.handleStatusBarChange);
  }
});
</script>

<style scoped>
.entrance {
  height: 100vh;
  width: 100%;
  background-image: url('../assets/background.png');
  background-size: auto 100%;
  background-position: center;
  background-attachment: fixed;
  justify-content: start;
  align-items: center;
  display: flex;
  flex-direction: column;
}

/* 顶部按钮区域样式 */
.top-buttons {
  display: flex;
  height: 60px;
  width: 100%;
  justify-content: space-between;
  gap: 8px
}

.top-image {
  height: 40px;
  width: 40px;
}

.right-buttons-group {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

/* 中间内容区域样式 */
.middle-content {
  position: absolute;
  flex: 1;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
}

.main_image {
  width: auto;
  height: 130px;
  margin-bottom: 30px;
}

.button-container {
  height: 27px;
  display: flex;
  gap: 20px;
  flex-direction: row;
  align-items: center;
}

.arrowhead {
  height: 14px;
}

.options {
  height: 20px;
}
</style>
